package simulatie.models;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;

import javax.vecmath.TexCoord2f;
import javax.vecmath.Vector3f;

/**
 * De ModelReader
 * @author TetraCON
 *
 */
	public class ModelReader {
	
	private FileReader reader;
	private String path;
	private String file;
	private int lastPos;
	private Hashtable<Integer,Vector3f> vertices, normals;
	private Hashtable<Integer,TexCoord2f> texCoords;
	private Hashtable<Integer,FaceIndex> faceIndicies;
	
/**
 * Constructor van de ModelReader - met standaard path
 */	
	public ModelReader() {
		this("D:\\Projects\\Java\\Containing\\Containing\\res\\containerheel.cpp");
	}
	
/**
 * Constructor
 * @param path - de path waar de ModelReader moet zoeken
 */	public ModelReader(String path) {
		this.path = path;
		char tmp[] = new char[80000];
		lastPos = 0;
		try {
			
			reader = new FileReader(this.path);
			reader.read(tmp);
			file = new String(tmp);
			
			setFaceIndicies();
			setVertices();
			setNormals();
			setTextCoords();
		}
		catch (FileNotFoundException fnfe) {
			fnfe.printStackTrace();
		}
		catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

/**
 * Zet alle FaceIndicies in een Hashtable
 */
 private void setFaceIndicies() {
		faceIndicies = new Hashtable<Integer,FaceIndex>();
		String faceIndex;
		
		lastPos = file.indexOf("face_indicies[", lastPos+1)+13;
		String sNumFace = file.substring(lastPos+1, file.indexOf("]", lastPos+1));
		int numFace = Integer.parseInt(sNumFace); 
		lastPos = file.indexOf("{", lastPos+1);
		for (int i = 0; i < numFace; i++) {
			faceIndex = file.substring(file.indexOf("{", lastPos+1)+1, file.indexOf("}", file.indexOf("{", lastPos+1)+1));
			faceIndex = faceIndex.replaceAll(" ", "");
			lastPos = file.indexOf("}", file.indexOf("{", lastPos+1)+1);
//			System.out.println(faceIndex + " : " + i);
			
			int cnt = -1;
			int index[] = new int[9];
			int j = 0;
			while (faceIndex.indexOf(",", cnt+1) != -1) {
				index[j] = Integer.parseInt(faceIndex.substring(cnt+1, faceIndex.indexOf(",", cnt+1)));
				cnt = faceIndex.indexOf(",", cnt+1);
				j++;
			}
			index[8] = Integer.parseInt(faceIndex.substring(faceIndex.lastIndexOf(",")+1, faceIndex.length()));
			
			faceIndicies.put(faceIndicies.size(), new FaceIndex(index));
		}		
	}
	
 /**
  * Zet alle Vertices in een Hashtable
  */
	private void setVertices() {
		vertices = new Hashtable<Integer,Vector3f>();
		String vertex;
		
		lastPos = file.indexOf("vertices [", lastPos+1)+9;
		String sNumVert = file.substring(lastPos+1, file.indexOf("]", lastPos+1));
//		System.out.println(sNumVert);
		int numVert = Integer.parseInt(sNumVert); 
		lastPos = file.indexOf("{", lastPos+1);
		for (int i = 0; i < numVert; i++) {
			vertex = file.substring(file.indexOf("{", lastPos+1)+1, file.indexOf("}", file.indexOf("{", lastPos+1)+1));
			vertex = vertex.replaceAll(" ", "");
			lastPos = file.indexOf("}", file.indexOf("{", lastPos+1)+1);
//			System.out.println(vertex + " : " + i);
			
			int cnt = -1;
			float v[] = new float[3];
			int j = 0;
			while (vertex.indexOf(",", cnt+1) != -1) {
				v[j] = Float.parseFloat(vertex.substring(cnt+1, vertex.indexOf(",", cnt+1)));
				cnt = vertex.indexOf(",", cnt+1);
				j++;
			}
			v[2] = Float.parseFloat(vertex.substring(vertex.lastIndexOf(",")+1, vertex.length()));
			
			vertices.put(vertices.size(), new Vector3f(v));
		}
	}

/**
 * Zet alle Normals in een Hashtable
 */
	private void setNormals() {
		normals = new Hashtable<Integer,Vector3f>();
		String normal;
		
		lastPos = file.indexOf("normals [", lastPos+1)+8;
		String sNumNorms = file.substring(lastPos+1, file.indexOf("]", lastPos+1));
//		System.out.println(sNumNorms);
		int numNorms = Integer.parseInt(sNumNorms); 
		lastPos = file.indexOf("{", lastPos+1);
		for (int i = 0; i < numNorms; i++) {
			normal = file.substring(file.indexOf("{", lastPos+1)+1, file.indexOf("}", file.indexOf("{", lastPos+1)+1));
			normal = normal.replaceAll(" ", "");
			lastPos = file.indexOf("}", file.indexOf("{", lastPos+1)+1);
//			System.out.println(normal + " : " + i);
			
			int cnt = -1;
			float v[] = new float[3];
			int j = 0;
			while (normal.indexOf(",", cnt+1) != -1) {
				v[j] = Float.parseFloat(normal.substring(cnt+1, normal.indexOf(",", cnt+1)));
				cnt = normal.indexOf(",", cnt+1);
				j++;
			}
			v[2] = Float.parseFloat(normal.substring(normal.lastIndexOf(",")+1, normal.length()));
			
			normals.put(normals.size(), new Vector3f(v));
		}
	}

/**
 * Zet alle TextCoords in een Hashtable
 */
	private void setTextCoords() {
		texCoords = new Hashtable<Integer,TexCoord2f>();
		String texCoord;
		
		lastPos = file.indexOf("textures [", lastPos+1)+9;
		String sNumTex = file.substring(lastPos+1, file.indexOf("]", lastPos+1));
//		System.out.println(sNumTex);
		int numTex = Integer.parseInt(sNumTex); 
		lastPos = file.indexOf("{", lastPos+1);
		for (int i = 0; i < numTex; i++) {
			texCoord = file.substring(file.indexOf("{", lastPos+1)+1, file.indexOf("}", file.indexOf("{", lastPos+1)+1));
			texCoord = texCoord.replaceAll(" ", "");
			lastPos = file.indexOf("}", file.indexOf("{", lastPos+1)+1);
//			System.out.println(texCoord + " : " + i);
			
			int cnt = -1;
			float v[] = new float[2];
			int j = 0;
			while (texCoord.indexOf(",", cnt+1) != -1) {
				v[j] = Float.parseFloat(texCoord.substring(cnt+1, texCoord.indexOf(",", cnt+1)));
				cnt = texCoord.indexOf(",", cnt+1);
				j++;
			}
			v[1] = Float.parseFloat(texCoord.substring(texCoord.lastIndexOf(",")+1, texCoord.length()));
			texCoords.put(texCoords.size(), new TexCoord2f(v));
		}
		
	}
	
/**
 * Vraag de FaceIndicies op
 * @return - Hashtable met de Faceindicies
 */	
	public Hashtable<Integer,FaceIndex> getFaceIndicies() {
		return faceIndicies;
	}
	
/**
 * Vraag de Vertices op
 * @return - Hashtable met de Vertices
 */	
	public Hashtable<Integer,Vector3f> getVertices() {
		return vertices;
	}
	
/**
 * Vraag de Normals op
 * @return - Hashtable met de Normals
 */	
	public Hashtable<Integer,Vector3f> getNormals() {
		return normals;
	}
	
/**
 * Vraag de TextCoords op
 * @return - Hashtable met de TextCoords
 */	
	public Hashtable<Integer,TexCoord2f> getTexCoords() {
		return texCoords;
	}

}
